package com.vhall.component.service.preparation.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.preparation.ScrollingMapper;
import com.vhall.component.entity.preparation.ScrollingEntity;
import com.vhall.component.entity.preparation.vo.ScrollingVO;
import com.vhall.component.service.preparation.ScrollingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author dongsong
 */
@Slf4j
@Service
public class ScrollingServiceImpl implements ScrollingService {

    @Autowired
    private ScrollingMapper scrollingMapper;


    @Override
    public ScrollingVO getScroll(String roomId) {
        ScrollingEntity scrollingEntity = getScrolling(roomId);
        ScrollingVO scrollingVO = new ScrollingVO();
        if (Objects.isNull(scrollingEntity)) {
            return null;
        }
        BeanUtils.copyProperties(scrollingEntity, scrollingVO);
        return scrollingVO;
    }


    /**
     * 获取跑马灯
     *
     * @param roomId
     * @return
     */
    private ScrollingEntity getScrolling(String roomId) {
        LambdaQueryWrapper<ScrollingEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ScrollingEntity::getRoomId, roomId);
        wrapper.isNull(ScrollingEntity::getDeletedAt);
        wrapper.last("limit 1;");
        return scrollingMapper.selectOne(wrapper);
    }
}
